package com.ksd.pug.service.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ksd.orm.pug.mapper.ArticleUserCollectMapper;
import com.ksd.pug.commons.threadlocal.UserThrealLocal;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.pojo.Article;
import com.ksd.pug.pojo.ArticleUserCollect;
import com.ksd.pug.pojo.SysLoginUser;
import com.ksd.pug.utils.redis.PugRedisOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: hcl
 * @date: 2022/1/24 14:59
 * @Description:
 */
@Service
@Slf4j
public class ArticleUserCollectServiceImpl extends ServiceImpl<ArticleUserCollectMapper, ArticleUserCollect> implements IArticleUserCollectService{

    @Autowired
    private PugRedisOperator redisOperator;

    @Autowired
    private ArticleServiceImpl articleService;
    //收藏文章
    @Override
    public int getCollect(Long articleId) {
        SysLoginUser user = UserThrealLocal.get();
        ArticleUserCollect articleUserCollect = new ArticleUserCollect();
        articleUserCollect.setUserId(user.getId());
        articleUserCollect.setArticleId(articleId);
        Article byId = articleService.getById(articleId);
        articleUserCollect.setArticleName(byId.getName());
        articleUserCollect.setArticleTime(byId.getCreateTime());
        this.save(articleUserCollect);
        return 0;
    }

    //取消收藏文章
    @Override
    public int removeCollect(Long articleId) {
        SysLoginUser user = UserThrealLocal.get();
        LambdaQueryWrapper<ArticleUserCollect> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ArticleUserCollect::getArticleId,articleId);
        lambdaQueryWrapper.eq(ArticleUserCollect::getUserId,user.getId());
        this.remove(lambdaQueryWrapper);
        return 0;
    }

    //查询用户收藏
    @Override
    public List<ArticleUserCollect> userCollectList() {
        SysLoginUser user = UserThrealLocal.get();
        LambdaQueryWrapper<ArticleUserCollect> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ArticleUserCollect::getUserId,user.getId());
        //lambdaQueryWrapper.eq(ArticleUserCollect::getUserId,1508545908096884737L);
        lambdaQueryWrapper.orderByDesc(ArticleUserCollect::getCreateTime);
        List<ArticleUserCollect> list = this.list(lambdaQueryWrapper);
        list.forEach(article->{
            String countStr = redisOperator.getHashValue("redis:user:zan:like",article.getArticleId()+"");
            if(Vsserts.isNotEmpty(countStr)) {
                article.setZannum(Integer.valueOf(countStr));
            }
            else {
                article.setZannum(0);
            }
            String countStr2 = redisOperator.getHashValue("redis:user:collect:like",article.getArticleId()+"");
            if(Vsserts.isNotEmpty(countStr2)) {
                article.setCollections(Integer.valueOf(countStr2));
            }
            else {
                article.setCollections(0);
            }
            //获取评论数
            String s = redisOperator.get("article:comment:count:" + article.getArticleId());
            if(Vsserts.isNotEmpty(s)){
                article.setPingluns(Integer.parseInt(s));
            }else {
                article.setPingluns(0);
            }

        });
        return list;
    }

}
